package github.fafaovo.common.event;

import github.fafaovo.common.config.exampleconfig;
import github.fafaovo.common.mycapability.ISpeedUpCapability;
import github.fafaovo.common.world.ExampleWorldSaveData;
import github.fafaovo.core.init.EnchantmentInit;
import github.fafaovo.core.registry.CommonSetupEventHandler;
import github.fafaovo.network.NetworkRegistryHandler;
import github.fafaovo.network.Packet.PowerClinetForServer;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
public class JoinWorldEvent {
    public static int count = 0;
    static int power = 0;
    public static int wCount = 0;
    public static final int wMaxPower = 100;
    @SubscribeEvent
    public static void onJoinWorld(TickEvent.PlayerTickEvent event)
    {
        if(event.phase == TickEvent.Phase.START && event.side.isServer())
        {
            power = ExampleWorldSaveData.get(event.player.getEntityWorld()).getPower();
            if(wCount > 100 * power)
            {
                wCount = 0;
                if(power < wMaxPower)
                    ExampleWorldSaveData.get(event.player.getEntityWorld()).setPower(power + 1);
            }
        }
        if(event.phase == TickEvent.Phase.START && event.side.isClient())
        {
            count++;
            if(count > 100)
            {
                wCount++;
                count = 0;
                //int amount = 0;
                PlayerEntity playerEntity = event.player;

                int finalAmount = getAmount(playerEntity,power);
                if(finalAmount < 0)
                    finalAmount = 0;
                LazyOptional<ISpeedUpCapability> capability = playerEntity.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
                int finalAmount1 = finalAmount;
                capability.ifPresent((c)->{
                    float examplePower = c.getLevel();
                    if(exampleconfig.example_boolean.get())
                    {
                        int maxPower = exampleconfig.max_power.get();
                        if(examplePower + finalAmount1 < maxPower)
                            c.setLevel(examplePower + finalAmount1);
                        else
                            c.setLevel(maxPower);
                        //System.out.println(c.getLevel());
                    }
                    else
                    {
                        if(examplePower - finalAmount1 <= 0)
                            c.setLevel(0);
                        else
                            c.setLevel(examplePower - finalAmount1);
                    }

                    NetworkRegistryHandler.CHANNEL.sendToServer(new PowerClinetForServer(c.getLevel()));
                });

            }
        }


    }

    public static int getAmount(PlayerEntity entity , int power)
    {
        EquipmentSlotType[] M = new EquipmentSlotType[]{
                EquipmentSlotType.FEET,
                EquipmentSlotType.LEGS,
                EquipmentSlotType.CHEST,
                EquipmentSlotType.HEAD
        };
        int leavl = 0;
        for (EquipmentSlotType equipmentSlotType : M) {
            ItemStack heldItemMainhand = entity.getItemStackFromSlot(equipmentSlotType);
            leavl += EnchantmentHelper.getEnchantmentLevel(EnchantmentInit.EXAMPLE_ENCHANTMENT.get(), heldItemMainhand);
        }
        boolean bool = exampleconfig.example_boolean.get(); //true 增加
        int amount = exampleconfig.example_integer.get(); //增加的值

        return bool ? amount - leavl + power : amount + leavl - power;
    }
}
